<template>
  <ft-dialog :visible="dialogVisible" @close="closeDialog" title="部门信息" width="80%">
    <div slot="title">
      <div>
        <span style="font-weight: bold;padding-right: 5px">部门信息</span>
      </div>
    </div>
    <el-card style="background-color: #F4F8F9;">
      <el-tag
        v-if="item.check"
        style="margin-right: 10px;margin-top: 10px;"
        :key="item.deptId"
        v-for="(item,index) in dataAll"
        closable
        :disable-transitions="false" @close="closeTag(item,index)">
        {{item.deptName}}
      </el-tag>
    </el-card>
    <el-card style="background-color: #F4F8F9;">
      <el-checkbox-group>
        <el-checkbox :label="item.deptName" v-for="(item,index) in dataAll" v-if="!item.check" :key="item.deptId"
                     @change="checkDept(item,index)"></el-checkbox>
      </el-checkbox-group>
    </el-card>
    <div slot="footer">
      <el-button size="small" type="primary" @click="save">确定</el-button>
    </div>
  </ft-dialog>
</template>

<script>
  export default {
    name: "UserDeptChoice",
    props: ['userId', 'orgId'],
    mounted() {
      this.load()
      this.loadChoice()
    },
    data() {
      return {
        dialogVisible: false,
        dataAll: [],
        data: [],
        checkList: []
      }
    },
    methods: {
      closeDialog() {
        this.dialogVisible = false
        this.$emit('closeDialog')
      },
      loadChoice() {
        if (this.orgId) {
          this.$http.get('/joys-rest/joys-master-data/dept/org/' + this.orgId).then(resp => {
            this.dataAll = resp.body
            this.dataAll.forEach((item) => {
              item.check = false
              for (var i = 0; i < this.data.length; i++) {
                if (item.deptId == this.data[i].deptId) {
                  item.check = true
                  break;
                }
              }
            })
          }, resp => {
            this.$message.error(resp.body.message)
          })
        }
      },
      load() {
        if (this.userId) {
          this.$http.get('/joys-rest/joys-master-data/userDepts/query/dept/' + this.userId).then(resp => {
            this.data = resp.body
            this.loadChoice()
          }, resp => {
            this.$message.error(resp.body.message)
          })
        }
      },
      save() {
        let userDtpts = []
        this.dataAll.forEach((item, index) => {
          if (item.check) {
            let userDept = {}
            userDept.deptId = item.deptId
            userDept.userId = this.userId
            userDtpts.push(userDept)
          }
        })
        this.$http.post('/joys-rest/joys-master-data/userDepts/saveBatch/' + this.userId, userDtpts).then(resp => {
          this.dialogVisible = false
          this.$emit('closeDialog')
          this.$message.success('保存成功')
        }, resp => {
          this.$message.error(resp.body.message)
        })
      },
      closeTag(value, index) {
        value.check = false
        this.$set(this.dataAll, index, value)
      },
      checkDept(value, index) {
        value.check = true
        this.$set(this.dataAll, index, value)
      }
    }
  }
</script>

<style scoped>

</style>
